package edu.zju.gis.dbfg.common.base;

import edu.zju.gis.dbfg.common.Result;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author Hu
 * @param <ID>
 */
@ToString
@Getter
@Setter
public abstract class BaseEntity<ID extends Serializable> implements Serializable {

    ID id;

    public static <T extends BaseEntity> Result encode(T body, Integer code, String message) {
        return new Result<>(body, code, message);
    }

    public static <T extends BaseEntity> T decode(Result r, Class<T> clz) throws Exception {
        if (r == null) {
            throw new NullPointerException("Input Result is null");
        }
        Object body = r.getBody();
        try {
            return (T) body;
        } catch (Exception e){
            throw new Exception("Object cannot be transformed to class " + clz.toString());
        }
    }

}
